package com.yanqu.road.dao.impl.activity.dunhuangshiku;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuKillLogData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/19 15:51
 */
public class DunHuangShiKuKillLogDataDaoImpl extends TempDataObjectDao<DunHuangShiKuKillLogData> {

    public List<DunHuangShiKuKillLogData> getDunHuangShiKuKillLogData(int activityId, int logTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<DunHuangShiKuKillLogData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall("select * from t_c_dunhuangshiku_kill_log_data where activityId=? and logTime > ? order by logTime desc ");
            int i = 1;
            call.setLong(i++,activityId);
            call.setInt(i++,logTime);
            rs = call.executeQuery();
            while (rs.next()) {
                DunHuangShiKuKillLogData dunHuangShiKuKillLogData = parse(rs);
                list.add(dunHuangShiKuKillLogData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String insertDunHuangShiKuKillLogDataSql = "insert into t_c_dunhuangshiku_kill_log_data(activityId,type,userId,attackerId,killType,targetId,targetServerId,targetName,damage,realKillId,eventId,bookNum,reward,killNum,logTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(DunHuangShiKuKillLogData dunHuangShiKuKillLogData) {
        dunHuangShiKuKillLogData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuKillLogData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuKillLogData.getType()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuKillLogData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuKillLogData.getAttackerId()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuKillLogData.getKillType()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuKillLogData.getTargetId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuKillLogData.getTargetServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, dunHuangShiKuKillLogData.getTargetName()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuKillLogData.getDamage()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuKillLogData.getRealKillId()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuKillLogData.getEventId()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuKillLogData.getBookNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, dunHuangShiKuKillLogData.getReward()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuKillLogData.getKillNum()));
        para.put(i++, new DbParameter(Types.INTEGER, dunHuangShiKuKillLogData.getLogTime()));
        if (getDb().executeNoneQuery(insertDunHuangShiKuKillLogDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected boolean updateData(DunHuangShiKuKillLogData data) {
        return false;
    }

    @Override
    protected DunHuangShiKuKillLogData parseData(ResultSet rs) throws SQLException {DunHuangShiKuKillLogData dunHuangShiKuKillLogData = new DunHuangShiKuKillLogData();
        dunHuangShiKuKillLogData.setActivityId(rs.getInt("activityId"));
        dunHuangShiKuKillLogData.setType(rs.getByte("type"));
        dunHuangShiKuKillLogData.setUserId(rs.getLong("userId"));
        dunHuangShiKuKillLogData.setAttackerId(rs.getLong("attackerId"));
        dunHuangShiKuKillLogData.setKillType(rs.getByte("killType"));
        dunHuangShiKuKillLogData.setTargetId(rs.getLong("targetId"));
        dunHuangShiKuKillLogData.setTargetServerId(rs.getLong("targetServerId"));
        dunHuangShiKuKillLogData.setTargetName(rs.getString("targetName"));
        dunHuangShiKuKillLogData.setDamage(rs.getLong("damage"));
        dunHuangShiKuKillLogData.setRealKillId(rs.getLong("realKillId"));
        dunHuangShiKuKillLogData.setEventId(rs.getInt("eventId"));
        dunHuangShiKuKillLogData.setBookNum(rs.getInt("bookNum"));
        dunHuangShiKuKillLogData.setReward(rs.getString("reward"));
        dunHuangShiKuKillLogData.setKillNum(rs.getInt("killNum"));
        dunHuangShiKuKillLogData.setLogTime(rs.getInt("logTime"));

        return dunHuangShiKuKillLogData;
    }
}
